/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.newcontent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.newcontent.param.*;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.newcontent.entity.BizNewContent;
import vip.xiaonuo.biz.modular.newcontent.mapper.BizNewContentMapper;
import vip.xiaonuo.biz.modular.newcontent.service.BizNewContentService;

import java.util.List;

/**
 * 内容管理Service接口实现类
 *
 * @author wangxy
 * @date  2024/08/14 11:47
 **/
@Service
public class BizNewContentServiceImpl extends ServiceImpl<BizNewContentMapper, BizNewContent> implements BizNewContentService {

    @Override
    public Page<BizNewContent> page(BizNewContentPageParam bizNewContentPageParam) {
        QueryWrapper<BizNewContent> queryWrapper = new QueryWrapper<BizNewContent>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getTitle())) {
            queryWrapper.lambda().like(BizNewContent::getTitle, bizNewContentPageParam.getTitle());
        }
        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getColumnId())) {
            queryWrapper.lambda().eq(BizNewContent::getColumnId, bizNewContentPageParam.getColumnId());
        }
        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getStatus())) {
            queryWrapper.lambda().eq(BizNewContent::getStatus, bizNewContentPageParam.getStatus());
        }

        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getStartCreateTime()) && ObjectUtil.isNotEmpty(bizNewContentPageParam.getEndCreateTime())) {
            queryWrapper.lambda().between(BizNewContent::getCreateTime, bizNewContentPageParam.getStartCreateTime(), bizNewContentPageParam.getEndCreateTime());
        }
        if(ObjectUtil.isAllNotEmpty(bizNewContentPageParam.getSortField(), bizNewContentPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizNewContentPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizNewContentPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizNewContentPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizNewContent::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizNewContentAddParam bizNewContentAddParam) {
        BizNewContent bizNewContent = BeanUtil.toBean(bizNewContentAddParam, BizNewContent.class);
        // 默认状态
        bizNewContent.setStatus("0");
        this.save(bizNewContent);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizNewContentEditParam bizNewContentEditParam) {
        BizNewContent bizNewContent = this.queryEntity(bizNewContentEditParam.getId());
        BeanUtil.copyProperties(bizNewContentEditParam, bizNewContent);
        this.updateById(bizNewContent);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizNewContentIdParam> bizNewContentIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizNewContentIdParamList, BizNewContentIdParam::getId));
    }

    @Override
    public BizNewContent detail(BizNewContentIdParam bizNewContentIdParam) {
        return this.queryEntity(bizNewContentIdParam.getId());
    }

    @Override
    public BizNewContent queryEntity(String id) {
        BizNewContent bizNewContent = this.getById(id);
        if(ObjectUtil.isEmpty(bizNewContent)) {
            throw new CommonException("内容管理不存在，id值为：{}", id);
        }
        return bizNewContent;
    }

    @Override
    public void apply(BizNewContentApplyParam bizNewContentApplyParam) {
        BizNewContent bizNewContent = this.queryEntity(bizNewContentApplyParam.getId());
        BeanUtil.copyProperties(bizNewContentApplyParam, bizNewContent);
        this.updateById(bizNewContent);
    }

    @Override
    public Page<BizNewContent> applyPage(BizNewContentPageParam bizNewContentPageParam) {
        QueryWrapper<BizNewContent> queryWrapper = new QueryWrapper<BizNewContent>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getTitle())) {
            queryWrapper.lambda().like(BizNewContent::getTitle, bizNewContentPageParam.getTitle());
        }
        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getColumnId())) {
            queryWrapper.lambda().eq(BizNewContent::getColumnId, bizNewContentPageParam.getColumnId());
        }
            queryWrapper.lambda().gt(BizNewContent::getStatus, 0);

        if(ObjectUtil.isNotEmpty(bizNewContentPageParam.getStartCreateTime()) && ObjectUtil.isNotEmpty(bizNewContentPageParam.getEndCreateTime())) {
            queryWrapper.lambda().between(BizNewContent::getCreateTime, bizNewContentPageParam.getStartCreateTime(), bizNewContentPageParam.getEndCreateTime());
        }
        if(ObjectUtil.isAllNotEmpty(bizNewContentPageParam.getSortField(), bizNewContentPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizNewContentPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizNewContentPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizNewContentPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(BizNewContent::getCreateTime);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

}
